package com.hefei.garden.product;

import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;

import java.util.concurrent.TimeUnit;

public class Product {
    public static void main(String[] args) throws PulsarClientException {

        // 创建Pulsar客户端连接
        PulsarClient client = PulsarClient.builder()
                .serviceUrl("pulsar://192.168.92.110:6650")
                .build();

        // 创建生产者
        Producer<String> producer = client.newProducer(Schema.STRING)
                .topic("my-topic")
                .create();

        int i = 0;
        try {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                String message = "Message " + i;
                System.out.println(message);
                producer.send(message);
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭生产者和Pulsar客户端
            producer.close();
            client.close();
        }

    }
}
